package com.handtransaction.controller;

import com.handtransaction.entity.Administrator;
import com.handtransaction.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.multipart.MultipartFile;

import javax.jws.WebParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SessionAttributes(value = {"admin"})//将model中属性名为admin的属性copy一份添加到会话中
@Controller
public class AdminController {
    @Autowired
    @Qualifier("administratorImpl")
    private AdministratorService adminService;

    //管理员登录
    @RequestMapping("/adminLogin")
    public String adminLogin(HttpSession session , String aid , String apassword, Model model){
        try{
            Integer adminId = Integer.valueOf(aid);
            Administrator admin = adminService.adminLogin(adminId);
            if(admin == null){
                //数据库中不存在该管理员，或用户名错误
                model.addAttribute("msg","数据库中不存在该管理员，或用户名错误");
                return "admin/adminLogin";
            }
            else if(!admin.getApassword().equals(apassword)){
                //密码错误
                model.addAttribute("msg","密码错误");
                model.addAttribute("adminId",admin.getAid());
                return "admin/adminLogin";
            }
            else {//登录成功
                model.addAttribute("admin",admin); //存入session中
                return "admin/adminConsole";
            }
        }catch (Exception e){
            model.addAttribute("msg" , "只限输入数字");
            return "admin/adminLogin";
        }
    }

    //管理员退出登录
    @RequestMapping("/adminLoginOut")
    public String adminLoginOut(HttpSession session , SessionStatus sessionStatus){
        session.removeAttribute("admin");
        sessionStatus.setComplete();
        return"redirect:ShowALLGoods";
    }

    //管理员查看个人信息
    @RequestMapping("/readAdminInfo")
    public String readAdminInfo(){
        return "admin/adminInfo";
    }

    //超级管理员查看所有普通管理员
    @RequestMapping("/showAllGeneralAdmins")
    public String showAllGeneralAdmins(Model model){
        List<Administrator> adminList = adminService.showAllGeneralAdmin();
        model.addAttribute("adminList" , adminList);
        return "admin/allAdmins";
    }

    //查找普通管理员
    @RequestMapping("queryAdmin")
    public String queryAdmin(String idOrName , Model model){
        List<Administrator> adminList = new ArrayList<>();
        adminList.add(adminService.queryGeneralAdmin(idOrName));
        model.addAttribute("adminList" , adminList);
        return "admin/allAdmins";
    }

    //管理员修改个人信息
    @RequestMapping("/updateAdminInfo")
    public String updateAdminInfo(Administrator admin, Model model, @RequestParam(value = "aheadimg") MultipartFile file
            , HttpServletRequest request) {
        try{
            //上传的头像图片保存路径设置
            String path = request.getServletContext().getRealPath("/imgs/adminImgs");
            File realPath = new File(path);
            if(!realPath.exists()){
                realPath.mkdir();
            }
            //上传头像图片写入项目目录下
            file.transferTo(new File(realPath+"/"+file.getOriginalFilename()));
            //将上传的头像图片保存的路径存入数据库中
            admin.setAheadsrc("imgs/adminImgs/"+file.getOriginalFilename());
        }catch (Exception e){
            e.printStackTrace();
        }
        adminService.updateAdminInfo(admin);
        //修改信息后的最新管理员个人信息覆盖掉当前的session中的admin
        model.addAttribute("admin" , adminService.queryAdmin(admin));
        return "admin/adminConsole";
    }

    //管理员修改密码 -- 跳转页面
    @RequestMapping("/toAdminPasswordPage")
    public String toAdminPasswordPage(){
        return "admin/adminPassword";
    }

    //管理员修改密码
    @RequestMapping("/updateAdminPassword")
    public String updateAdminPassword(Administrator admin){
        Administrator temp = adminService.queryAdmin(admin);
        temp.setApassword(admin.getApassword());
        adminService.updateAdminInfo(temp);
        return "admin/adminLogin";
    }

    //跳转到增加管理员页面
    @RequestMapping("/toCreateAdminPage")
    public String toCreateAdminPage(){
        return "admin/newAdmin";
    }

    //增加管理员
    @RequestMapping("/createAdmin")
    public String createAdmin(Administrator admin , Model model){
        adminService.createAdmin(admin);
        return "redirect:/showAllGeneralAdmins";
    }

    //删除管理员
    @RequestMapping("/deleteAdmin")
    public String deleteAdmin(Integer aid){
        adminService.deleteAdmin(aid);
        return "redirect:/showAllGeneralAdmins";
    }



}
